package com.servlet.user;

import com.enitiy.User;
import com.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/28 12:08
 * 项目名称 SHUSHAN
 */
@WebServlet("/UserUpdate")
public class UserUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserServiceImpl usi = new UserServiceImpl();
        int num = Integer.parseInt(req.getParameter("num"));
        User user = usi.UserSelectByNum(num);
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        if(pwd==null||pwd.length()<=0){
            pwd=user.getPwd();
        }
        int age =  Integer.parseInt(req.getParameter("age"));
        String sex = req.getParameter("sex");
        if (sex==null||sex.length()<=0){
            sex=user.getSex();
        }
        String phone = req.getParameter("phone");
        if (phone==null||phone.length()<=0){
            phone=user.getPhone();
        }
        int kid =Integer.parseInt(req.getParameter("kid"));
        boolean b = usi.UserUpdateByNum(new User(num, name, pwd, age, sex, phone, kid));
        if (b){
            System.out.println("修改成功！");
            User user1 = usi.UserSelectByNum(num);

            String img = user1.getImg();
            if (img==null||img.length() <= 0){
                System.out.println(img);
                img="1.jpg";
                user1.setImg(img);
            }
            req.setAttribute("user",user1);
            req.getRequestDispatcher("/page/user/usermain.jsp").forward(req,resp);
        }else {
            System.out.println("修改失败！");
            resp.sendRedirect("/page/user/userUpdate.jsp");
        }


    }
}
